from django import template
from ftbexplorer.core.models import Sentence
from ftbexplorer.fnd.linguistique.tests import est_connecteur
from ftbexplorer.fnd.linguistique import BaseClause, Clause, WordClause, ConnectorClause, PartialClause
from ftbexplorer.fnd.linguistique.extracteurs import Sentence2Clause

register = template.Library()

@register.filter
def highlight_connectors(sent):
    assert isinstance(sent, Sentence)
    text = []
    for w in sent.words():
        if est_connecteur(w):
            text.append(u"<span class=\"connecteur\">%s</span>" % w)
        else:
            text.append(u"%s" % w)
    return u" ".join(text)

def clause_to_html(cl):
    assert isinstance(cl, BaseClause), "Type is %s " % type(cl)
    sub = map(clause_to_html, list(cl))
    if isinstance(cl, Clause):
        return u"<span class=\"clause\">%s</span>" % u" ".join(sub)
    elif isinstance(cl, ConnectorClause):
        return u"<span class=\"connecteur\">%s</span>" % unicode(cl)
    elif isinstance(cl, WordClause):
        return unicode(cl)
    else:
        return u" ".join(sub)

@register.filter
def clauses(sent):
    assert isinstance(sent, Sentence)
    clause = Sentence2Clause(sent)
    return u"<p>%s</p>" % clause_to_html(clause)

